package cn.iocoder.boot.job;

import cn.iocoder.boot.common.RedisKeyConstants;
import cn.iocoder.boot.service.KbPlatformApiService;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class APICallResetHandler implements JobHandler {
    @Resource
    private KbPlatformApiService kbPlatformApiService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String execute(String param) throws Exception {
        log.info("【定时任务】开始重置平台API调用次数");
        kbPlatformApiService.list().forEach(api -> {
            api.setRemainingNumber(api.getCallNumber());
            kbPlatformApiService.updateById(api);
            //删除redis缓存
            stringRedisTemplate.delete(String.format(RedisKeyConstants.PLATFORM_API_TEMPLATE, api.getUserId()));
        });
        log.info("【定时任务】结束重置平台API调用次数");
        return "success";
    }
}
